//
//  BIDClockManager.m
//  theFifth
//
//  Created by zd2011 on 13-6-9.
//
//

#import "BIDClockManager.h"
#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
#import "BIDDatabaseManager.h"
#define kClockTable @"clockTable"
@implementation BIDClockManager
+(BIDClockManager *)shareManager
{
    static BIDClockManager *clockManager=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        clockManager=[[BIDClockManager alloc] init];
    });
    return clockManager;
}
-(BOOL)addClock:(BIDAClock *)clcok
{
    BOOL judge=NO;
    //,time text,type_id integer ,subType_id integer,other text
    NSString *insert=[NSString stringWithFormat:@"insert into %@(type_id,subType_id,time,other,amount) values(%d,%d,'%@','%@',%f)",kClockTable,clcok.type_ID,clcok.subType_ID,clcok.time,clcok.other,clcok.amount];
 judge= [[BIDDatabaseManager SharedOperator].fmdbDataBase executeUpdate:insert];
    return judge;
}
-(BOOL)deleteClock:(BIDAClock *)clock
{
    BOOL judge=NO;
    //,time text,type_id integer ,subType_id integer,other text clock_ID
    NSString *insert=[NSString stringWithFormat:@"delete from %@ where clock_ID=%d",kClockTable,clock.clock_id];
    judge= [[BIDDatabaseManager SharedOperator].fmdbDataBase executeUpdate:insert];
    return judge;
}
-(NSMutableArray *)listOfClocks
{
    NSMutableArray *aray=[[NSMutableArray alloc] init];
    NSString *list=[NSString stringWithFormat:@"select *from %@ order by time DESC",kClockTable];
   FMResultSet *res=[[BIDDatabaseManager SharedOperator].fmdbDataBase executeQuery:list];

    while ([res next]) {
        BIDAClock *clock=[[BIDAClock alloc] init];
        clock.amount=[res doubleForColumn:@"amount"];
        clock.clock_id=[res intForColumn:@"clock_ID"];
        clock.type_ID=[res intForColumn:@"type_id"];
        clock.subType_ID=[res intForColumn:@"subType_id"];
        clock.time=[res stringForColumn:@"time"];
        clock.other=[res stringForColumn:@"other"];
        [aray addObject:clock];
        [clock release];
    }
    return aray;
    [aray release];
}
-(NSInteger)countOfClocks
{
    NSInteger count=0;
    NSString *list=[NSString stringWithFormat:@"select count(*) from %@",kClockTable];
    FMDatabase *fm=[BIDDatabaseManager SharedOperator].fmdbDataBase;
    count=[fm intForQuery:list];
    return count;
}
-(BOOL)deleteCurrentClocks
{// select * from( ) limit 0,2
    BOOL judge=NO;
    NSArray *time=[[self getCurrntTime] componentsSeparatedByString:@"分"];
    NSString *delete=[NSString stringWithFormat:@"delete from %@ where time<='%@'",kClockTable,[time objectAtIndex:0]];
    judge=[[BIDDatabaseManager SharedOperator].fmdbDataBase executeUpdate:delete];
    return judge;
}
-(NSString *)getCurrntTime
{
    NSDate* now = [NSDate date];
    NSDateFormatter *formaton=[[NSDateFormatter alloc] init];
   
    [formaton setDateFormat:@"YYYY-MM-dd HH:mm:ss +0800"];
    
    NSString *str=[formaton stringFromDate:now];
    return str;
}
-(NSMutableArray *)listOfUsedClocks:(NSInteger)iconNumber
{
     NSArray *time=[[self getCurrntTime] componentsSeparatedByString:@"+"];
    NSMutableArray *array=[[NSMutableArray alloc] init];
    NSString *str=[NSString stringWithFormat:@"select  *  from %@  where time <='%@'   order by time DESC",kClockTable,[time objectAtIndex:0]];
    NSString *useful=[NSString stringWithFormat:@"select *from (%@) limit 0,%d",str,iconNumber];
    FMResultSet *res=[[BIDDatabaseManager SharedOperator].fmdbDataBase executeQuery:useful];
    
    while ([res next]) {
        BIDAClock *clock=[[BIDAClock alloc] init];
        clock.amount=[res doubleForColumn:@"amount"];
        clock.clock_id=[res intForColumn:@"clock_ID"];
        clock.type_ID=[res intForColumn:@"type_id"];
        clock.subType_ID=[res intForColumn:@"subType_id"];
        clock.time=[res stringForColumn:@"time"];
        clock.other=[res stringForColumn:@"other"];
        [array addObject:clock];
        [clock release];
    }
    return array;
    [array release];
}
@end
